<template>
  <el-form ref="sbglFormRef" :model="sbglform" :rules="rules" label-width="130px" style="margin-bottom: 12px;">
    <h3>过滤</h3>
    <!-- 修改prop和v-model字段名 -->
<!--    <el-form-item label="过滤不包邮" prop="jhIsPost">-->
<!--      <el-checkbox v-model="sbglform.jhIsPost"/>-->
<!--    </el-form-item>-->
    <!-- 新增过滤所有违规字段 -->
<!--    <el-form-item label="过滤所有违规" prop="jhIsGotOut">-->
<!--      <el-checkbox v-model="sbglform.jhIsGotOut"/>-->
<!--    </el-form-item>-->
<!--    &lt;!&ndash; 新增过滤无SKU字段 &ndash;&gt;-->
<!--    <el-form-item label="过滤无SKU" prop="jhIsSku">-->
<!--      <el-checkbox v-model="sbglform.jhIsSku"/>-->
<!--    </el-form-item>-->
<!--    &lt;!&ndash; 新增过滤上市季节 &ndash;&gt;-->
<!--    <el-form-item label="过滤上市季节" prop="jhSeason">-->
<!--      <el-checkbox-group v-model="sbglform.jhSeason">-->
<!--        <el-checkbox label="春">春</el-checkbox>-->
<!--        <el-checkbox label="夏">夏</el-checkbox>-->
<!--        <el-checkbox label="秋">秋</el-checkbox>-->
<!--        <el-checkbox label="冬">冬</el-checkbox>-->
<!--      </el-checkbox-group>-->
<!--    </el-form-item>-->
    <!-- 新增类目过滤选择框 -->
<!--    <el-form-item label="类目过滤" prop="jhClass">-->
<!--      <el-select-->
<!--        v-model="sbglform.jhClass"-->
<!--        multiple-->
<!--        filterable-->
<!--        placeholder="请选择"-->
<!--        style="width: 100%">-->
<!--        <el-option-->
<!--          v-for="item in jhClassOptions"-->
<!--          :key="item.jhId"-->
<!--          :label="item.name"-->
<!--          :value="item.jhId">-->
<!--        </el-option>-->
<!--      </el-select>-->
<!--    </el-form-item>-->
    <!-- 新增品牌过滤输入框 -->
    <el-form-item label="品牌过滤" prop="jhKeywordBrandStr">
      <div style="position: relative;">
        <textarea
          v-model="sbglform.jhKeywordBrandStr"
          :rows="3"
          placeholder="宝贝在选中的品牌中将不发布，多个品牌以换行符分隔，一行一个品牌"
          style="width: 100%"
          type="textarea"
        />
        <i class="el-icon-delete"
           style="position: absolute; right: 10px; top: 50%; transform: translateY(-50%); cursor: pointer;"
           @click="clearBrand"
        ></i>
      </div>
    </el-form-item>

    <!-- 新增店铺过滤字段 -->
    <el-form-item label="店铺过滤" prop="jhKeywordShopStr">
      <div style="position: relative;">
        <textarea  v-model="sbglform.jhKeywordShopStr" :rows="3" placeholder="宝贝在选中的店铺中将不发布，多个店铺以换行符分隔，一行一个店铺名" style="width: 100%" type="textarea"/>
        <i class="el-icon-delete"
           style="position: absolute; right: 10px; top: 50%; transform: translateY(-50%); cursor: pointer;"
           @click="clearShop"
        ></i>
      </div>
    </el-form-item>

    <!-- 新增标题过滤字段 -->
    <el-form-item label="标题过滤" prop="jhKeywordTitleStr">
      <div style="position: relative;">
        <textarea  v-model="sbglform.jhKeywordTitleStr" :rows="3"
                  placeholder="情趣用品阴肛双插性自熨器女入体蓝牙跳蛋肛门情趣菊花女用插屁股肛赛尾巴外出长期佩戴盯赛后庭玩县女用肛交专用工具拉珠情趣内衣制服诱惑性感小野猫兔女郎套装睡衣小胸纯欲狐狸尾巴睡衣按摩棒仿真男性假生植器硅胶超大号道具鸡鸡模型情趣大鸡鸡假机八假阴基全自动伸缩加热仿真阴基吸盘坐女用新奇性玩具情趣用品成"
                  style="width: 100%" type="textarea"/>
        <i class="el-icon-delete"
           style="position: absolute; right: 10px; top: 50%; transform: translateY(-50%); cursor: pointer;"
           @click="clearTitle"
        ></i>
      </div>
    </el-form-item>

    <!-- 新增描述过滤字段 -->
<!--    <el-form-item label="描述过滤" prop="jhKeywordDescStr">-->
<!--      <div style="position: relative;">-->
<!--        <el-input v-model="sbglform.jhKeywordDescStr" :rows="3" placeholder="宝贝在选中的描述中将不发布，多个描述以换行符分隔，一行一个描述或描述关键字" style="width: 100%"-->
<!--                  type="textarea"/>-->
<!--        <i class="el-icon-delete"-->
<!--           style="position: absolute; right: 10px; top: 50%; transform: translateY(-50%); cursor: pointer;"-->
<!--           @click="clearDesc"-->
<!--        ></i>-->
<!--      </div>-->
<!--    </el-form-item>-->

<!--    &lt;!&ndash; 新增SKU过滤字段 &ndash;&gt;-->
<!--    <el-form-item label="SKU过滤" prop="jhKeywordSkuStr">-->
<!--      <div style="position: relative;">-->
<!--        <el-input v-model="sbglform.jhKeywordSkuStr" :rows="3" placeholder="SKU关键词，多个关键词以换行符分隔，一行一个SKU关键字" style="width: 100%" type="textarea"/>-->
<!--        <i class="el-icon-delete"-->
<!--           style="position: absolute; right: 10px; top: 50%; transform: translateY(-50%); cursor: pointer;"-->
<!--           @click="clearSku"-->
<!--        ></i>-->
<!--      </div>-->
<!--    </el-form-item>-->

<!--    &lt;!&ndash; 新增SKU过滤操作字段 &ndash;&gt;-->
<!--    <el-form-item label="SKU过滤操作" prop="jhSkuType">-->
<!--      <el-radio-group v-model="sbglform.jhSkuType">-->
<!--        <el-radio :label="0">根据关键词过滤SKU</el-radio>-->
<!--        <el-radio :label="1">根据关键词过滤整个商品</el-radio>-->
<!--      </el-radio-group>-->
<!--    </el-form-item>-->

    <!-- 新增价格区间字段 -->
    <el-form-item label="只复制此价格区间的宝贝" label-style="{ whiteSpace: 'nowrap' }" prop="jhPriceStart">
      <el-row>
        <el-col :span="8">
          <el-input v-model.number="sbglform.jhPriceBegin" placeholder="开始价格" type="number"/>
        </el-col>
        <el-col :span="2" style="text-align: center; padding: 0 12px;">
          -
        </el-col>
        <el-col :span="8">
          <el-input v-model.number="sbglform.jhPriceEnd" placeholder="结束价格" type="number"/>
        </el-col>
      </el-row>
    </el-form-item>

    <!-- 新增价格区间字段后添加 -->
    <el-form-item label="总库存少于此值不复制" prop="jhInventoryNum">
      <el-input v-model.number="sbglform.jhInventoryNum" placeholder="请输入最小库存阈值" type="number"/>
    </el-form-item>

    <!-- 新增总库存字段后添加的6个字段 -->
    <el-form-item label="SKU价格低于等于此值的SKU不复制" prop="jhSkuLessEqual">
      <el-input v-model.number="sbglform.jhSkuLess" placeholder="请输入数值" type="number"/>
    </el-form-item>
    <el-form-item label="SKU价格高于等于此值的SKU不复制" prop="jhSkuGreaterEqual">
      <el-input v-model.number="sbglform.jhSkuGreater" placeholder="请输入数值" type="number"/>
    </el-form-item>
    <el-form-item label="SKU库存少于等于此值的SKU不复制" prop="jhSkuStockLessEqual">
      <el-input v-model.number="sbglform.jhSkuEqual" placeholder="请输入数值" type="number"/>
    </el-form-item>
    <el-form-item label="销量少于此值不复制" prop="jhSalesNumLess">
      <el-input v-model.number="sbglform.jhSalesNum" placeholder="请输入数值" type="number"/>
    </el-form-item>
    <el-form-item label="过滤详情图数量少于" prop="jhPictureNumLess">
      <el-input v-model.number="sbglform.jhPictureNum" placeholder="请输入数值" type="number"/>
    </el-form-item>
    <el-form-item label="过滤标题英文字母多于" prop="jhLetterNumMore">
      <el-input v-model.number="sbglform.jhLetterNum" placeholder="请输入数值" type="number"/>
    </el-form-item>

  </el-form>
</template>
<script>
import {getTeFiCa} from "@/api/jihuo/template";

export default {
  dicts: ['jh_class'],
  name: "sbglform",
  data() {
    return {
      sbglform: {
        jhString001: null,
        jhIsPost: false,  // 确保初始值明确为布尔类型
        // 修改初始化方式为布尔值
        jhIsGotOut: false,
        jhIsSku: false,
        jhSeason: [],
        jhClass: [],
        jhKeywordBrandStr: null,
        jhKeywordBrand: {},
        // 新增字段初始化
        jhKeywordShopStr: null,
        jhKeywordShop: {},
        jhKeywordTitleStr: null,
        jhKeywordTitle: {},
        jhKeywordDescStr: null,
        jhKeywordDesc: {},
        jhKeywordSkuStr: null,
        jhKeywordSku: {},
        jhPriceStart: null, // 新增字段
        jhPriceEnd: null,   // 新增字段
        jhInventoryNum: null, // 新增字段
        jhSkuType: 0,  // 默认值设置为单选按钮的第一个值
      },
      rules: {
        jhRevision: [
          {required: true, message: "乐观锁不能为空", trigger: "blur"}
        ],
        jhCreatedBy: [
          {required: true, message: "创建人不能为空", trigger: "blur"}
        ],
        jhCreatedTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        jhUpdatedBy: [
          {required: true, message: "更新人不能为空", trigger: "blur"}
        ],
        jhUpdatedTime: [
          {required: true, message: "更新时间不能为空", trigger: "blur"}
        ],
        jhTemplateName: [
          {required: true, message: "模版名称不能为空", trigger: "blur"}
        ],
        jhCode: [
          {required: true, message: "商家编码不能为空", trigger: "blur"}
        ],
        // 添加 jhIsPost 的验证规则
        jhIsPost: [
          {type: 'boolean', message: '必须为布尔类型'}
        ],
      },
      jhClassOptions:[],
    }
  },
  mounted() {
    this.fetchCategoryData();
  },
  methods: {
    fetchCategoryData() {
      getTeFiCa().then(response => {
        this.jhClassOptions = response.data
      }).catch(error => {
        console.log("获取类目数据失败: ", error)
      });

    },
    // 新增setData方法用于接收父组件数据
    setData(form) {
      this.sbglform = {
        // 确保布尔值转换
        // 修正赋值逻辑
        // 使用全等判断确保正确转换
        jhIsPost: form.jhIsPost === true || form.jhIsPost === 'true', // 兼容布尔和字符串
        jhIsGotOut: form.jhIsGotOut === true || form.jhIsGotOut === 'true',
        jhIsSku: form.jhIsSku === true || form.jhIsSku === 'true',
        jhSeason: form.jhSeason ? form.jhSeason.split(',') : [],
        jhClass: form.jhClass ? (Array.isArray(form.jhClass) ? form.jhClass : form.jhClass.split(',')) : [],
        // 处理jhKeywordBrand：如果是数组则转换为换行符分隔的字符串，否则直接使用
        jhKeywordBrandStr: Array.isArray(form.jhKeywordBrand) ?
          form.jhKeywordBrand.join('\n') : (form.jhKeywordBrand || ''),
        // 处理jhKeywordShop：如果是数组则转换为换行符分隔的字符串，否则直接使用
        jhKeywordShopStr: Array.isArray(form.jhKeywordShop) ?
          form.jhKeywordShop.join('\n') : (form.jhKeywordShop || ''),
        // 处理jhKeywordTitle：如果是数组则转换为换行符分隔的字符串，否则直接使用
        jhKeywordTitleStr: Array.isArray(form.jhKeywordTitle) ?
          form.jhKeywordTitle.join('\n') : (form.jhKeywordTitle || ''),
        // 处理jhKeywordDesc：如果是数组则转换为换行符分隔的字符串，否则直接使用
        jhKeywordDescStr: Array.isArray(form.jhKeywordDesc) ?
          form.jhKeywordDesc.join('\n') : (form.jhKeywordDesc || ''),
        // 处理jhKeywordSku：如果是数组则转换为换行符分隔的字符串，否则直接使用
        jhKeywordSkuStr: Array.isArray(form.jhKeywordSku) ?
          form.jhKeywordSku.join('\n') : (form.jhKeywordSku || ''),
        // 修复赋值逻辑，确保数值类型
        jhSkuType: Number(form.jhSkuType) || 0,  // 强制转换为数字类型
        jhPriceBegin: form.jhPriceBegin || null,
        jhPriceEnd: form.jhPriceEnd || null,
        jhInventoryNum: form.jhInventoryNum || null,
        jhSkuLess: form.jhSkuLess || null,
        jhSkuGreater: form.jhSkuGreater || null,
        jhSkuEqual: form.jhSkuEqual || null,
        jhSalesNum: form.jhSalesNum || null,
        jhPictureNum: form.jhPictureNum || null,
        jhLetterNum: form.jhLetterNum || null
      };
    },
    getFormData() {
      return {
        ...this.sbglform,
        // 处理多选字段为字符串
        jhSeason: this.sbglform.jhSeason?.join(',') || '',
        // jhClass保持为数组，不转换为字符串
        jhClass: this.sbglform.jhClass || [],
        // 将jhKeywordBrandStr按换行符切割成数组赋值给jhKeywordBrand
        jhKeywordBrand: this.sbglform.jhKeywordBrandStr ?
          this.sbglform.jhKeywordBrandStr.split('\n').filter(item => item.trim() !== '') : [],
        // 将jhKeywordShopStr按换行符切割成数组赋值给jhKeywordShop
        jhKeywordShop: this.sbglform.jhKeywordShopStr ?
          this.sbglform.jhKeywordShopStr.split('\n').filter(item => item.trim() !== '') : [],
        // 将jhKeywordDescStr按换行符切割成数组赋值给jhKeywordDesc
        jhKeywordDesc: this.sbglform.jhKeywordDescStr ?
          this.sbglform.jhKeywordDescStr.split('\n').filter(item => item.trim() !== '') : [],
        // 将jhKeywordSkuStr按换行符切割成数组赋值给jhKeywordSku
        jhKeywordSku: this.sbglform.jhKeywordSkuStr ?
          this.sbglform.jhKeywordSkuStr.split('\n').filter(item => item.trim() !== '') : [],
        jhKeywordTitle: this.sbglform.jhKeywordTitleStr ?
          this.sbglform.jhKeywordTitleStr.split('\n').filter(item => item.trim() !== '') : []
      };
    },
    clearBrand() {
      this.sbglform.jhKeywordBrandStr = '';
    },
    // 新增清除方法
    clearShop() {
      this.sbglform.jhKeywordShopStr = '';
    },
    clearTitle() {
      this.sbglform.jhKeywordTitle = '';
    },
    clearDesc() {
      this.sbglform.jhKeywordDescStr = '';
    },
    clearSku() {
      this.sbglform.jhKeywordSkuStr = '';
    },
    // 修改reset方法
    reset() {
      this.sbglform = {
        jhIsPost: false, // 替换初始化字段
        jhIsSku: false,
        jhSeason: [], // 重置时初始化为空数组
        jhClass: [], // 重置时初始化为空数组
        jhKeywordBrandStr: null,
        jhKeywordBrand: {},
        // 新增字段重置
        jhKeywordShopStr: null,
        jhKeywordShop: {},
        jhKeywordTitle: null,
        jhKeywordDescStr: null,
        jhKeywordDesc: {},
        jhKeywordSkuStr: null,
        jhKeywordSku: {},
        jhPriceStart: null, // 新增字段重置
        jhPriceEnd: null,   // 新增字段重置
        jhInventoryNum: null, // 新增字段重置
        jhIsGotOut: false, // 新增字段重置
      };
    }
  }
}
</script>

<style scoped>
.el-form-item {
  margin-bottom: 12px !important; /* 原默认20px，缩小间距 */
}
</style>
